<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 配件ajax类
 * @package  	ajax
 * @subpackage  backend
 * @author   	create by allen.qiang
 */

quanli_require('module/base/production/productionOrder.class.php');

class ajax_orderRevise {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

                case 1: //查询订单
                    $this->searchProductionOrder();
                    break;
                case 2: //删除订单
                    $this->delete();
                    break;
                default:
                    backendError();
            }

        } else {
            backendError();
        }

    }
    /**
     * 查询订单
     */
    private function searchProductionOrder() {
        //查询条件选择了城市、配送站 则根据选择的城市、配送站查询
        if( isset($_REQUEST['cityID']) && is_numeric($_REQUEST['cityID']) && isset( $_REQUEST['distribution_id']) && is_numeric($_REQUEST['distribution_id'])){
            $_REQUEST['cityID'] = $_REQUEST['cityID'];
            $_REQUEST['distribution_id'] = $_REQUEST['distribution_id'];

            //查询条件选择了城市、没有选择配送站 则根据选择的城市、当前登录用户权限下的配送站查询
        }else if(isset($_REQUEST['cityID']) && is_numeric($_REQUEST['cityID']) && !isset( $_REQUEST['distribution_id']) || !is_numeric($_REQUEST['distribution_id']) ){
            $_REQUEST['cityID'] = $_REQUEST['cityID'];
            global $G_SHOP;
            $PS_LOGIN_SESSION =$G_SHOP->sessionCache_get('PS_LOGIN_SESSION');

            $distribution_id_array = $PS_LOGIN_SESSION['distribution'];//权限下的配送站id数组

            if( count($distribution_id_array) > 1){

                $distribution_id_arg = implode( ',',$distribution_id_array );

                $_REQUEST['extend'] = ' AND distribution_id IN ('.$distribution_id_arg.')';

            }else{
                $_REQUEST['distribution_id'] = $distribution_id_array[0];

            }

            //查询条件没有选择城市、没有选择配送站 则根据选择的城市、当前登录用户权限下的城市、配送站查询
        }else{
            global $G_SHOP;
            $PS_LOGIN_SESSION =$G_SHOP->sessionCache_get('PS_LOGIN_SESSION');

            $distribution_id_array = $PS_LOGIN_SESSION['distribution'];//权限下的配送站id数组

            if( count($distribution_id_array) > 1){

                $distribution_id_arg = implode( ',',$distribution_id_array );

                $_REQUEST['extend'] = ' AND distribution_id IN ('.$distribution_id_arg.')';

            }else{
                $_REQUEST['distribution_id'] = $distribution_id_array[0];

            }
        }

//        global $G_SHOP;
//
//        //获得用户的所属城市id
//        $PS_LOGIN_SESSION =$G_SHOP->sessionCache_get('PS_LOGIN_SESSION');
//
//        $city_id_array = $PS_LOGIN_SESSION['city'];//当前城市id
//
//        $city_id_length = count($city_id_array);
//        if( $city_id_length == 0){ //当前登录用户没有城市权限 查看不到数据
//            //组织返回的数据
//            $result_array = array(
//                'page' => 1,
//                'total' => 0,
//                'rows' => array()
//            );
//            echo json_encode($result_array);
//
//        }else if( $city_id_length > 1){
//            $str_city_id = implode( ',',$city_id_array );
//            $_REQUEST['extend'] = " AND city_id IN ( ".$str_city_id." )";
//        }else{
//            $_REQUEST['city_id'] = $PS_LOGIN_SESSION['city'][0];
//        }

        global $G_PGLOB;

        $specHandler = new productionOrder( $G_PGLOB->DBCA ) ;

        if( isset($_REQUEST['bill_status']) && is_numeric($_REQUEST['bill_status'])){
            $_REQUEST['bill_status'] = $_REQUEST['bill_status'];
        }else{
            $_REQUEST['extend2'] = ' AND bill_status IN (4,5,7)';
        }

       // $_REQUEST['bill_status'] = 4;//订单修改提示只查出库状态
        $result_data  = $specHandler->productionOrder( $_REQUEST ) ;

        $result_array = array(
            'page'  => $_REQUEST['page'] ,
            'total' => $_REQUEST['count'] ,
            'rows'  => array()
        );

        if ($result_data) {

            foreach($result_data as &$s_l) {
                if( $s_l['is_handle'] == 1 ){
                    $operating = '已处理';
                }else{
                    $operating = '<a href="javascript: void(0);" onclick="productionOrder.deleteConfirm(' . $s_l['notice_id'] . ')">未处理</a>&nbsp;';
                }
                    $result_array['rows'][] = array(
                        'id'   => $s_l['notice_id'],
                        'cell' => array(
                            $operating,
                            $s_l['notice_id'],
                            $s_l['bill_status'],
                            '<a href="/quanliadmin/app/bill/billSearch.pscript?operFlg=9&bill_no=' . $s_l['bill_no'] . '" target="_blank">' . $s_l['bill_code'] . '</a>',
                            $s_l['distribution_name'],
                            $s_l['goods_info'],
                            $s_l['alter_content'],
                        )
                    );
            }
        }

        echo json_encode( $result_array );
    }


    /**
     * 删除订单
     *
     */
    private function delete () {
        global $G_PGLOB;

        $specHandler = new productionOrder( $G_PGLOB->DBCA ) ;
        $result_array = $specHandler->delete( $_REQUEST ) ;

        echo json_encode($result_array);
    }

}